var page = 1;//页数
var total = 0;//总条数
var totalPage = 0;//总页数
var list=[];
$(function () {
    init(1);
    $("#pre").click(function () {
        if (page == parseInt(total/10)+1){$("#next").attr("disabled",false)};
        page = page-1;
        if (page<=0){page=1};
        init(page);
    })
    $("#next").click(function () {
        if (page == 1){$("#pre").attr("disabled",false)};
        page = page+1;
        if (page > (total/10)+1){page = (total/10)+1};
        init(page);
    });

})
function init(index){
    page=index;
    var html = `<tr>
                <th width="5%">序号</th>
                <th width="10%">姓名</th>
                <th width="10%">党派</th>
                <th width="20%">单位及职务</th>
                <th width="10%">代表级别</th>
                <th width="15%">所属届次</th>
                <th width="15%">代表团/代表小组</th> 
                <th width="10%">所属联络站名称</th>
                <th width="5%">操作区</th>
            </tr>`; //表格标题
    $.ajax({
        url:'./js/digitalPlatform/representative.json',
        type: 'GET',
        dataType: 'JSON',
        success: function (data) {
            var dbData = data.results;
            console.log(data);
            total = dbData.length;
            totalPage = parseInt(total / 10 + 1);
            $.each(dbData, function (index, val) {
                list.push(val);
                if (index >= (page - 1) * 10 && index < 10 * page) {
                    html += `<tr>
                <td class="align_center" >${index + 1}</td>
                <td class="align_center" >${val.pName}</td>
                <td class="align_center" >${val.pParty}</td>
                <td class="align_center" title="${val.pwork}">${val.pwork}</td>
                <td class="align_center" title="${val.pResp}">${val.pResp}</td>
                <td class="align_center" title="${val.pDate}">${val.pDate}</td>
                <td class="align_center" title="${val.pGroup}">${val.pGroup}</td>
                <td class="align_center" title="${val.pLlz}">${val.pLlz}</td>
                <td class="align_center" ><a href="javascript:;" onclick="info('${val.id}')">查看</a></td>
            </tr>`
                }
            })
            // console.log(page);
            $("#db").html(html);
            if (page == 1) {
                $("#pre").attr("disabled", true)
            } else {
                $("#pre").attr("disabled", false)
            }
            ;
            if (page == parseInt(total / 10) + 1) {
                $("#next").attr("disabled", true)
            } else {
                $("#next").attr("disabled", false)
            }
            ;
            pageLoad();
            // console.log(parseInt(total/15)+1)
        }
    })
}

function info(id) {
    console.log(id);
    var urldb='代表信息.html?id='+id;
    window.open(urldb);
}
//翻页功能
function pageLoad() {
    var i = 1;
    i = parseInt(i);
    var item="";
    if (totalPage <= 3 ) {//总页数小于五页，则加载所有页

        for (i; i <= totalPage; i++) {
            if (i == page) {
                item += "<span class='disabled'>"+i+"</span>";
            }else{
                item += "<span class='show' onclick='init("+i+")'>"+i+"</span>";
            }
        };
        $('#pageBtn').html(item);
        return;
    }else if (totalPage > 3) {//总页数大于五页，则加载五页
        if (page < 3) {//当前页小于5，加载1-5页
            for (i; i <= 3; i++) {
                if (i == page) {
                    item += "<span class='disabled'>"+i+"</span>";
                }else{
                    item += "<span class='show' onclick='init("+i+")'>"+i+"</span>";
                }
            };
            if (page <= totalPage-2) {//最后一页追加“...”代表省略的页
                item += "<span> . . . </span>";
                item += "<span class='show' onclick='init("+totalPage+")'>"+totalPage+"</span>"
            }
            $('#pageBtn').html(item);
            return;
        }else if (page >= 3) {//当前页大于5页
            for (i; i <= 1; i++) {//1,2页码始终显示
                item += "<span class='show' onclick='init("+i+")'>"+i+"</span>";
            }
            item += "<span> . . . </span>";//2页码后面用...代替部分未显示的页码
            if (page+1 == totalPage) {//当前页+1等于总页码
                for(i = page-1; i <= totalPage; i++){//“...”后面跟三个页码当前页居中显示
                    if (i == page) {
                        item += "<span class='disabled'>"+i+"</span>";
                    }else{
                        item += "<span class='show' onclick='init("+i+")'>"+i+"</span>";
                    }
                }
            }else if (page == totalPage) {//当前页数等于总页数则是最后一页页码显示在最后
                for(i = page-2; i <= totalPage; i++){//...后面跟三个页码当前页居中显示
                    if (i == page) {
                        item += "<span class='disabled'>"+i+"</span>";
                    }else{
                        item += "<span class='show' onclick='init("+i+")'>"+i+"</span>";
                    }
                }
            }else if (page+2 == totalPage) {//当前页+1等于总页码
                for(i = page-1; i <= totalPage; i++){//“...”后面跟三个页码当前页居中显示
                    if (i == page) {
                        item += "<span class='disabled'>"+i+"</span>";
                    }else{
                        item += "<span class='show' onclick='init("+i+")'>"+i+"</span>";
                    }
                }
            }else{//当前页小于总页数，则最后一页后面跟...
                for(i = page-1; i <= page+1; i++){//page+1页后面...
                    if (i == page) {
                        item += "<span class='disabled'>"+i+"</span>";
                    }else{
                        item += "<span class='show' onclick='init("+i+")'>"+i+"</span>";
                    }
                }
                item += "<span> . . . </span>";
                item += "<span class='show' onclick='init("+totalPage+")'>"+totalPage+"</span>"
            }
            $('#pageBtn').html(item);
            return;
        }
        // console.log(item);
    }

}





